package com.jungle.base.domain.vo;

import lombok.Getter;

@Getter
public class JsonResult<T> {
    private final int code;
    private String message;
    private T data;

    private JsonResult(int code, String message) {
        this.code = code;
        this.message = message;
    }

    private JsonResult(int code, T data) {
        this.code = code;
        this.data = data;
    }

    /**
     * 成功返回数据
     *
     * @param data 响应数据
     */
    public static <T> JsonResult<T> success(T data) {
        return new JsonResult<>(0, data);
    }

    /**
     * 操作执行成功
     */
    public static <T> JsonResult<T> success() {
        return new JsonResult<>(0, null);
    }

    /**
     * 操作失败，返回错误信息
     *
     * @param code    错误码
     * @param message 错误信息
     */
    public static <T> JsonResult<T> fail(int code, String message) {
        return new JsonResult<>(code, message);
    }

    /**
     * 操作失败，返回异常信息
     *
     * @param code      错误码
     * @param exception 异常
     */
    public static <T> JsonResult<T> fail(int code, Exception exception) {
        return new JsonResult<>(code, exception.getMessage());
    }
}
